package com.designoutput.manager
{
	import com.designoutput.ui.newControls.ControlBase;
	
	import flash.display.Stage;
	import flash.events.Event;

	public class ValidationManager
	{
		private var _stage:Stage;
		private var _validationQueue:Vector.<ControlBase> = new Vector.<ControlBase>();
		
		public function ValidationManager(stage:Stage)
		{
			_stage = stage;
		}
		
		public function queueValidation(controlBase:ControlBase):void {
			if(_validationQueue.indexOf(controlBase) == -1){
				if(_validationQueue.length == 0)
					addCallValidate();
					
				_validationQueue.push(controlBase);
			}
		}
		
		protected function validate():void {			
			while(_validationQueue.length > 0)
				_validationQueue.shift().validate();
		}
		
		protected function addCallValidate():void {
			_stage.addEventListener(Event.RENDER, callValidate, false, 0, true);
			_stage.addEventListener(Event.ENTER_FRAME, callValidate, false, 0, true);
			_stage.invalidate();
		}
		
		protected function callValidate(e:Event):void {
			_stage.removeEventListener(Event.RENDER, callValidate, false);
			_stage.removeEventListener(Event.ENTER_FRAME, callValidate, false);
			
			validate();
		}
	}
}